package com.dragee.modules.work.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragee.common.R;
import com.dragee.config.exception.BusinessException;
import com.dragee.config.shiro.AuthToken;
import com.dragee.constants.AuthConstants;
import com.dragee.modules.sys.dto.TokenOfWxDTO;
import com.dragee.modules.sys.entity.LoginUser;
import com.dragee.modules.sys.entity.UserEntity;
import com.dragee.modules.sys.po.RolePo;
import com.dragee.modules.sys.service.UserService;
import com.dragee.utils.TokenUtil;
import com.dragee.utils.weiXin.OpenIdAndSessionKey;
import com.dragee.utils.weiXin.WeiXinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author tys on 2023/5/24
 */
@RestController
@RequestMapping("work/weiXin")
@Api(tags = "work - 小程序")
public class WeiXinController {
    @Autowired
    private UserService userService;

    @GetMapping("getUserPhoneNumber")
    @ApiOperation("获取手机号")
    public R getUserPhoneNumber(@RequestParam("code") String code){
        String phoneNum = WeiXinUtil.getUserPhoneNumber(code);
        return R.ok().put("data", phoneNum);
    }

    @GetMapping("getRoleByNumber")
    @ApiOperation("根据手机号获取角色")
    public R getRoleByNumber(@RequestParam("phoneNumber") String phoneNumber){
        UserEntity use = userService.getOne(new QueryWrapper<UserEntity>()
                .eq("phone", phoneNumber), false);
        if (use  == null) {
            return R.ok().put("data", null);
        }
        RolePo role = userService.getHighRoleUp(use.getUserId().toString());
        return R.ok().put("data", role);

    }

    @GetMapping("getOpenId")
    @ApiOperation("获取小程序的token")
    public R getOpenId(String code){
        OpenIdAndSessionKey openIdAndSessionKey = WeiXinUtil.getOpenId(code);
        if (openIdAndSessionKey == null) {
            throw new BusinessException("获取openId失败");
        }
        LoginUser loginUser = new LoginUser();
        loginUser.setClientId("wx");
        loginUser.setWxOpenId(openIdAndSessionKey.getOpenId());
        String tokenStr = TokenUtil.createToken(loginUser, AuthConstants.TOKEN_EXPIRE);
        AuthenticationToken token = new AuthToken(tokenStr);
        Subject subject = SecurityUtils.getSubject();
        try  {
            subject.login(token);
            TokenOfWxDTO dto = new TokenOfWxDTO(AuthConstants.JWT_TOKEN_HEADER,
                    AuthConstants.JWT_TOKEN_PREFIX + tokenStr,
                    loginUser.getWxOpenId());
            return R.ok().put("data", dto);
        } catch (BusinessException e) {
            return R.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return R.error();
        }
    }

    @GetMapping("getQrCode")
    @ApiOperation("获取小程序二维码")
    public R getQrCode(String code){
       //  WeiXinUtil.getWXCode("123456", "D:/123.png");
        return R.ok();
    }



}
